package com.chap6;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 * <code>ExchangerDemo2</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-20 9:42
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class ExchangerDemo2 {
    private static final Exchanger<String> exchanger = new Exchanger<>();
    private static ExecutorService threadPool = Executors.newFixedThreadPool(2);

    public static void main(String[] args) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                String A = "12379871924sfkhfksdhfks";
                try {
                    exchanger.exchange(A);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                String B = "32423423jknjkfsbfj";
                try {
                    String A = exchanger.exchange(B);
                    System.out.println("A和B数据是否一致："+A.equals(B));
                    System.out.println("A= "+A);
                    System.out.println("B= "+B);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        threadPool.shutdown();
    }
}
